#include "CollisionChecker.h"
#include <algorithm>
#include "HeartHolder.h"

using namespace std;

CollisionChecker *CollisionChecker::collisionChecker;

CollisionChecker::CollisionChecker(void) {};

void CollisionChecker::Initialize(void) {
	if(collisionChecker == NULL)
		collisionChecker = new CollisionChecker();
}

CollisionChecker *CollisionChecker::GetSingleton(void) {
	return collisionChecker;
}

void CollisionChecker::Register(Sprite* s1, Sprite* s2){
    Pair p;
    p.first=s1;
    p.second=s2;
    pairs.push_back(p);
}

void CollisionChecker::RegisterHearts(Sprite* popeye_s) {
    Pair p;
    p.second = popeye_s;
	std::list<Hearts *>heartList = HeartHolder::getHeartList();
	std::list<Hearts *>::iterator i = heartList.begin();
	advance(i, HeartHolder::inForCheck);
	
	while(i!=heartList.end()) {
		p.first = (*i);
		pairs.push_back(p);
		++i;
		++HeartHolder::inForCheck;
	}
}

void CollisionChecker::Cancel(Sprite* s1, Sprite* s2) {}

void CollisionChecker::Check (void) const {
 	std::for_each(pairs.begin(), pairs.end(), CheckFunctor());
}

bool CollisionChecker::collidesWithBoarders(Sprite *s) {
	if(s->GetX()-10 < LEFT_BORDER) return true;
	if(s->GetX()+10 > RIGHT_BORDER) return true;

	return false;
}